//logs.js
var util = require('../../utils/util.js')
Page({
  data: {
     event: {
       name       :''
     },
     battle_id   : ''
  },
  //获取赛事信息
  onShow:function(){
      //赛事信息
      this.getEventInfo();
      var battle_info = wx.getStorageSync('battle_info');
      this.setData({
             battle_id   :battle_info.battle_id
      })
  },
   //请求服务器获取赛事的信息
   getEventInfo:function(){
      var that = this;
      var url  = getApp().data.servser+'/event/index';

      wx.request({
        url: url,
        data: {
          'id' : wx.getStorageSync('event_id')
        },
        header: {
            'content-type': 'application/json'
        },
        method : "GET",
        success: function(res) {
            that.setData({
                event: {
                    name       :res.data.event_info.event_name
                },
            })
        }
      })
   },
   
   doScanCode:function(){
    var that = this;
    if(this.remindAddBattleInfo()){
        wx.scanCode({
            success: (res) => {
                console.log(res.result);
                that.saveLoginCode(res.result);
            }
        })
     }
  },

  //如果对局信息为空提示用户添加对局信息
    remindAddBattleInfo:function(){
        var that =this;
        var battle_info = wx.getStorageSync('battle_info');
      
        if(!battle_info){
            that.setData({
              item: {
                 display    :'show',
                 error_info :'请添加对局信息'
              }
            })
            this.closeErrorInfo();
            return false; 
        }else{
            return true;
        }
    },

     //定时器关闭提示信息
    closeErrorInfo:function(){
        var that = this;
        setTimeout(function(){
          that.setData({
              item: {display:'hide'}
              })
        },'1000')
    },

     //如果对局信息为空提示用户添加对局信息
    remindAddBattleInfo:function(){
        var that =this;
        var battle_info = wx.getStorageSync('battle_info');
      
        if(!battle_info){
            that.setData({
              item: {
                 display    :'show',
                 error_info :'请添加对局信息'
              }
            })
            this.closeErrorInfo();
            return false; 
        }else{
            return true;
        }
    },

     //保存扫描后的登陆码
  saveLoginCode:function(login_code){
         var that = this;
      //添加登录码
         var url  = getApp().data.servser+'/Battle/addLoginCode';
          wx.request({
            url: url,
            data: {
              'id'         : that.data.battle_id,
              'login_code' : login_code,
              'consumption_id':wx.getStorageSync('consumption_id')
            },
            header: {
                'content-type': 'application/json'
            },
            method : "GET",
            success: function(res) {
                
            }
          })
  },
})
